#!/usr/bin/perl
#addprinter.cgi

require './printer-lib.pl';
&init_config();
&ReadParse();
&error_setup($text{'error'});
if(!$in{'pname'}) {
	&error($text{'err_nopname'});
}
if(!$in{'driver'}) {
	&error($text{'err_nodriver'});
}
if (!($in{'driver'} =~ /^(\S+.ppd)$/)) {
	&error($text{'err_driverfmt'});
}
$driverpath="/usrvol/volume1/pdriver/";
$driverfile=$driverpath.$in{'driver'};
if(! -e $driverfile) {
	&error($text{'err_nodrvfile'});
}
else{
	`cp -af $driverfile /usr/share/cups/model/`;
}

#delete current printer
open(CP, "cat /etc/printcap |grep rm|");
my @curp = <CP>;
close(CP);

foreach (@curp){
	next if ($_ =~ /^#/);
	if ($_ =~ /(\S+)\|/) {
		`lpadmin -x $1`;
		if($?){
			&error($text{'err_failed1'});
		}
	}
}

#Now add new printer
$out = `lpadmin -p $in{'pname'} -v usb:/dev/usb/lp0 -m $in{'driver'} -E`;
if($?) {
	$out = `lpadmin -x $in{'pname'}`;
	&error($text{'err_failed2'});
}
else{
	&redirect("index.cgi");
}
